
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Threading;
using System.Security.Principal;

using My.Entities;
using My.Authentication;
using My.Web.Handler;
using My.Application.Cms;

//
public partial class P : Page
{
    public P(){}
    public P( IAccountUiStrategy accUi,ICmsUiStrategy cmsUi )
    {
        _accUi = accUi;
        _cmsUi = cmsUi;
    }
    public string Execute( string cmd )
    {
        Thread.CurrentPrincipal = CmsHandler.GetPrincipal();

        try{
            Command c = GetCmds(cmd);
            string s = c();
            c = null;
            return s;
        }catch( Exception e ){
            return _cmsUi.ErrMsg( e.Message );
        }
    }

  #region GetCmds(Private)
    private delegate string Command();
    private static Dictionary<string,Command> cmds = null;
    private Command GetCmds( string cmd )
    {
        if( null==cmds )
        {
            cmds = new Dictionary<string,Command>();

            /// 分类
            cmds.Add( "AddCategory",new Command(_cmsUi.AddCategory) );
            cmds.Add( "RemoveCategories",new Command(_cmsUi.RemoveCategories) );

            cmds.Add( "ModifyCategory",new Command(_cmsUi.ModifyCategory) );
            cmds.Add( "MoveCategories",new Command(_cmsUi.MoveCategories) );

            cmds.Add( "GetCategory",new Command(_cmsUi.GetCategory) );

            cmds.Add( "GetCategoryParent",new Command(_cmsUi.GetCategoryParent) );
            cmds.Add( "GetCategoryAncestors",new Command(_cmsUi.GetCategoryAncestors) );
            cmds.Add( "GetCategoryChildren",new Command(_cmsUi.GetCategoryChildren) );

            cmds.Add("GetCategories",new Command(_cmsUi.GetCategories) );

            /// 文章
            cmds.Add("AddArticle",new Command(_cmsUi.AddArticle) );
            cmds.Add("RemoveArticles",new Command(_cmsUi.RemoveArticles) );

            cmds.Add("ModifyArticle",new Command(_cmsUi.ModifyArticle) );
            cmds.Add("MoveArticles",new Command(_cmsUi.MoveArticles) );

            cmds.Add("GetArticle",_cmsUi.GetArticle);
            cmds.Add("GetArticles",_cmsUi.GetArticles);

            /// 文章评论
            cmds.Add("AddComment",_cmsUi.AddComment);
            cmds.Add("RemoveComments",_cmsUi.RemoveComments);

            cmds.Add("ModifyComment",_cmsUi.ModifyComment);
            cmds.Add("MoveComments",_cmsUi.MoveComments);

            cmds.Add("GetComment",_cmsUi.GetComment);
            cmds.Add("GetComments",_cmsUi.GetComments);
            
            // 账户
            cmds.Add("AddAccount",new Command(_accUi.AddAccount) );
            cmds.Add("RemoveAccounts",new Command(_accUi.RemoveAccounts) );

            cmds.Add("ModifyAccount",new Command(_accUi.ModifyAccount) );
            cmds.Add("MoveAccountsTo",new Command(_accUi.MoveAccountsTo) );

            cmds.Add("GetAccount",_accUi.GetAccount);
            cmds.Add("GetAccounts",_accUi.GetAccounts);
        }
        if( cmds.ContainsKey(cmd) )
            return cmds[cmd];
        else
            throw new ArgumentException( "没有找到请求的命令" );
    }
  #endregion GetCmds(Private)
  
  #region Private Member
    private IAccountUiStrategy _accUi = null;
    private ICmsUiStrategy _cmsUi = null;
  #endregion Private Member
}

#region Cms
// 策略接口,此接口仅服务于格式统一的界面,如Json,xml等,对于html界面,应该直接调用CmsHandler类自行构建
public interface ICmsUiStrategy
{
  #region 分类
    string AddCategory();
    string RemoveCategories();

    string ModifyCategory();
    string MoveCategories();

    string GetCategory();

    string GetCategoryParent();
    string GetCategoryAncestors();
    string GetCategoryChildren();

    string GetCategories();
  #endregion 分类

  #region 文章
    string AddArticle();
    string RemoveArticles();

    string ModifyArticle();
    string MoveArticles();

    string GetArticle();
    string GetArticles();
  #endregion 文章

  #region 文章评论
    string AddComment();
    string RemoveComments();

    string ModifyComment();
    string MoveComments();

    string GetComment();
    string GetComments();
  #endregion 文章评论

    string ErrMsg( string message );
}

// 实际策略--Json
public class CmsUiJson : ICmsUiStrategy
{
    private CmsHandler hdl = null;

    public CmsUiJson( object handler )
    {
        hdl = (CmsHandler)handler;
    }
  #region 分类
    public string AddCategory()
    {
        hdl.AddCategory();
        //if( 0 < i ){
            return Msg("分类添加成功! ");
        //}else{
        //    return ErrMsg("分类添加失败! ");
        //}
    }
    public string RemoveCategories()
    {
        int i = hdl.RemoveCategories();
        
        if( 0<i ){
            return Msg( string.Format("成功删除分类,受影响分类{0}项",i.ToString() ) );
        }else{
            return ErrMsg("分类删除失败:被删除的分类下不能有子分类,同时不能有文章关联！要删除该分类,请先移走上述内容!");
        }
    }

    public string ModifyCategory()
    {
        int i = hdl.ModifyCategory();
        if( 0 < i ){
            return Msg("分类修改成功! ");
        }else{
            return ErrMsg("分类修改失败! ");
        }
    }
    public string MoveCategories()
    {
        int i = hdl.MoveCategories();
        if( 0 < i ){
            return Msg("分类移动成功! ");
        }else{
            return ErrMsg("分类移动失败! ");
        }
    }

    public string GetCategory()
    {
        CategoryInfo info = hdl.GetCategory();
        return info.ToString( "json" );
    }

    public string GetCategoryParent()
    {
        CategoryInfo info = hdl.GetCategoryParent();
        return info.ToString( "json" );
    }
    public string GetCategoryAncestors()
    {
        IList<CategoryInfo> infos = hdl.GetCategoryAncestors();
        return ToString( infos );
    }
    public string GetCategoryChildren()
    {
        IList<CategoryInfo> infos = hdl.GetCategoryChildren();
        return ToString( infos );
    }

    public string GetCategories()
    {
        IList<CategoryInfo> infos = hdl.GetCategoriesByQuery( 0 );
        return ToString( infos );
    }
  #endregion 分类

  #region 文章
    public string AddArticle()
    {
        hdl.AddArticle();
        return Msg("文章添加成功!");
    }
    public string RemoveArticles()
    {
        int i = hdl.RemoveArticles();
        if( 0<i ){
            return Msg("删除成功!");
        }else{
            return ErrMsg("删除失败:未知原因!");
        }
    }

    public string ModifyArticle()
    {
        int i = hdl.ModifyArticle();
        if( 0 < i ){
            return Msg("文章修改成功!");
        }
        else{
            return ErrMsg("文章修改失败!");
        }
    }
    public string MoveArticles()
    {
        int i = hdl.MoveArticles();
        if( 0 < i ){
            return Msg("文章移动成功!");
        }
        else{
            return ErrMsg("文章移动失败!");
        }
    }

    public string GetArticle()
    {
        ArticleInfo info = hdl.GetArticle();
        return info.ToString( "json" );
    }
    public string GetArticles()
    {
        int defaultCatId = 0;
        My.PageInfo defaultPage = new My.PageInfo( 10,1 );
        string defaultOptions = "OrderDESC";
        IList<ArticleInfo> infos = hdl.GetArticlesByQuery( defaultCatId,defaultPage,defaultOptions );

        return string.Concat( "{'Content':",ToString(infos),",'Page':",defaultPage.ToString("json"),"}" );
    }
  #endregion 文章

  #region 文章评论
    public string AddComment()
    {
        hdl.AddComment();
        return Msg("评论添加成功!");
    }
    public string RemoveComments()
    {
        int i = hdl.RemoveComments();
        if( 0<i ){
            return Msg("删除成功!");
        }else{
            return ErrMsg("删除失败:未知原因!");
        }
    }

    public string ModifyComment()
    {
        int i = hdl.ModifyComment();
        if( 0 < i ){
            return Msg("文章修改成功!");
        }
        else{
            return ErrMsg("文章修改失败!");
        }
    }
    public string MoveComments()
    {
        int i = hdl.MoveComments();
        if( 0 < i ){
            return Msg("文章移动成功!");
        }
        else{
            return ErrMsg("文章移动失败!");
        }
    }

    public string GetComment()
    {
        ArticleInfo info = hdl.GetComment();
        return info.ToString( "json" );
    }
    public string GetComments()
    {
        int defaultCatId = 0;
        My.PageInfo defaultPage = new My.PageInfo( 10,1 );
        string defaultOptions = "OrderDESC,Detail";
        IList<ArticleInfo> infos = hdl.GetCommentsByQuery( -100,defaultPage,defaultOptions );
        return string.Concat( "{'Content':",ToString(infos),",'Page':",defaultPage.ToString("json"),"}" );
    }
  #endregion 文章评论

  #region
    public string ErrMsg( string message )
    {
        return string.Format( "{{'error':'{0}'}}",My.Convert.JsonEncode(message) );
    }
    private string Msg( string message )
    {
        return string.Format( "{{msg:'{0}'}}",My.Convert.JsonEncode(message) );
    }

    private string ToString<T>( IList<T> infos ) where T:IEntity
    {
        string[] ss = new string[infos.Count];
        int i = 0;
        foreach( T info in infos ){
            ss[i] = info.ToString( "json" ); ++i; }

        return string.Format( "[{0}]",string.Join(",",ss) );
    }
  #endregion
}

// 实际策略--Xml
// ...

// 实际策略--Html
public class CmsUiHtml : ICmsUiStrategy
{
    private CmsHandler hdl = null;

    public CmsUiHtml( object handler )
    {
        hdl = (CmsHandler)handler;
    }
  #region 分类
    public string AddCategory()
    {
        hdl.AddCategory();
        //if( 0 < i ){
            return Msg("分类添加成功! ");
        //}else{
        //    return ErrMsg("分类添加失败! ");
        //}
    }
    public string RemoveCategories()
    {
        int i = hdl.RemoveCategories();
        return Msg( string.Format("成功删除{0}项分类") );
        //if( 0<i ){
        //    return Msg("分类删除成功!");
        //}else{
        //    return ErrMsg("分类删除失败:未知原因!");
        //}
    }

    public string ModifyCategory()
    {
        int i = hdl.ModifyCategory();
        return "";
        if( 0 < i ){
            return Msg("分类修改成功! ");
        }else{
            return ErrMsg("分类修改失败! ");
        }
    }
    public string MoveCategories()
    {
        int i = hdl.MoveCategories();
        if( 0 < i ){
            return Msg("分类移动成功! ");
        }else{
            return ErrMsg("分类移动失败! ");
        }
    }

    public string GetCategory()
    {
        CategoryInfo info = hdl.GetCategory();
        return info.ToString( "json" );
    }

    public string GetCategoryParent()
    {
        CategoryInfo info = hdl.GetCategoryParent();
        return info.ToString( "json" );
    }
    public string GetCategoryAncestors()
    {
        IList<CategoryInfo> infos = hdl.GetCategoryAncestors();
        return ToString( infos );
    }
    public string GetCategoryChildren()
    {
        IList<CategoryInfo> infos = hdl.GetCategoryChildren();
        return ToString( infos );
    }

    public string GetCategories()
    {
        IList<CategoryInfo> infos = hdl.GetCategoriesByQuery( 0 );
        return ToString( infos );
    }
  #endregion 分类

  #region 文章
    public string AddArticle()
    {
        hdl.AddArticle();
        return Msg("文章添加成功!");
    }
    public string RemoveArticles()
    {
        int i = hdl.RemoveArticles();
        if( 0<i ){
            return Msg("删除成功!");
        }else{
            return ErrMsg("删除失败:未知原因!");
        }
    }

    public string ModifyArticle()
    {
        int i = hdl.ModifyArticle();
        if( 0 < i ){
            return Msg("文章修改成功!");
        }
        else{
            return ErrMsg("文章修改失败!");
        }
    }
    public string MoveArticles()
    {
        int i = hdl.MoveArticles();
        if( 0 < i ){
            return Msg("文章移动成功!");
        }
        else{
            return ErrMsg("文章移动失败!");
        }
    }

    public string GetArticle()
    {
        ArticleInfo info = hdl.GetArticle();
        return info.ToString( "json" );
    }
    public string GetArticles()
    {
        int defaultCatId = 0;
        My.PageInfo defaultPage = new My.PageInfo( 10,1 );
        string defaultOptions = "OrderDESC";
        IList<ArticleInfo> infos = hdl.GetArticlesByQuery( defaultCatId,defaultPage,defaultOptions );
        return ToString( infos );
    }
  #endregion 文章

  #region 文章评论
    public string AddComment()
    {
        hdl.AddComment();
        return Msg("评论添加成功!");
    }
    public string RemoveComments()
    {
        int i = hdl.RemoveComments();
        if( 0<i ){
            return Msg("删除成功!");
        }else{
            return ErrMsg("删除失败:未知原因!");
        }
    }

    public string ModifyComment()
    {
        int i = hdl.ModifyComment();
        if( 0 < i ){
            return Msg("文章修改成功!");
        }
        else{
            return ErrMsg("文章修改失败!");
        }
    }
    public string MoveComments()
    {
        int i = hdl.MoveComments();
        if( 0 < i ){
            return Msg("文章移动成功!");
        }
        else{
            return ErrMsg("文章移动失败!");
        }
    }

    public string GetComment()
    {
        ArticleInfo info = hdl.GetComment();
        return info.ToString( "json" );
    }
    public string GetComments()
    {
        int defaultCatId = 0;
        My.PageInfo defaultPage = new My.PageInfo( 10,1 );
        string defaultOptions = "OrderDESC";
        IList<ArticleInfo> infos = hdl.GetComments( -100,defaultPage,null );
        return ToString( infos );
    }
  #endregion 文章评论


    public string ErrMsg( string message )
    {
        return string.Format( "{{'error':'{0}'}}",My.Convert.JsonEncode(message) );
    }
    private string Msg( string message )
    {
        return string.Format( "{{msg:'{0}'}}",My.Convert.JsonEncode(message) );
    }
    public string ToString<T>( IList<T> infos ) where T:IEntity
    {
        string[] ss = new string[infos.Count];
        int i = 0;
        foreach( IEntity info in infos ){
            ss[i] = info.ToString( "html" ); ++i; }

        return string.Format( "[{0}]",string.Join(",",ss) );
    }
}
#endregion

#region Account
public interface IAccountUiStrategy
{
    string AddAccount();
    string RemoveAccounts();
    string ModifyAccount();
    string ChangePassword();
    string MoveAccountsTo();
    string IsAccountExists();
    string GetAccount();
    string GetAccounts();
}

public class AccountUiJson : IAccountUiStrategy
{
  #region 构造器
    private AccountHandler hdl = null;

    public AccountUiJson( object handler )
    {
        hdl = (AccountHandler)handler;
    }
  #endregion 构造器
  
  #region IAccountUiStrategy
    public string AddAccount()
    {
        hdl.AddAccount();
        return Msg("账户添加成功!");
    }

    public string RemoveAccounts()
    {
        int i = hdl.RemoveAccounts();
        if( 0<i ){
            return Msg("删除成功!");
        }else{
            return ErrMsg("删除失败:未知原因!");
        }
    }

    public string ModifyAccount()
    {
        int i = hdl.ModifyAccount();
        if( 0 < i ){
            return Msg("账户修改成功!");
        }
        else{
            return ErrMsg("账户修改失败!");
        }
    }

    public string ChangePassword()
    {
        throw new Exception( "未实现或不支持" );
    }

    public string MoveAccountsTo()
    {
        int i = hdl.MoveAccountsTo();
        if( 0 < i ){
            return Msg("账户移动成功!");
        }
        else{
            return ErrMsg("账户移动失败!");
        }
    }

    public string IsAccountExists()
    {
        throw new Exception( "未实现或不支持" );
    }

    public string GetAccount()
    {
        AccountInfo info = hdl.GetAccount();
        return info.ToString( "json" );
    }

    public string GetAccounts()
    {
        My.PageInfo defaultPage = new My.PageInfo( 10,1 );
        string defaultOptions = "OrderDESC";
        IList<AccountInfo> infos = hdl.GetAccountsByQuery( defaultPage,defaultOptions );

        return string.Concat( "{'Content':",ToString(infos),",'Page':",defaultPage.ToString("json"),"}" );
    }
  #endregion IAccountUiStrategy
  

  #region
    public string ErrMsg( string message )
    {
        return string.Format( "{{'error':'{0}'}}",My.Convert.JsonEncode(message) );
    }
    private string Msg( string message )
    {
        return string.Format( "{{msg:'{0}'}}",My.Convert.JsonEncode(message) );
    }

    private string ToString<T>( IList<T> infos ) where T:IEntity
    {
        string[] ss = new string[infos.Count];
        int i = 0;
        foreach( T info in infos ){
            ss[i] = info.ToString( "json" ); ++i; }

        return string.Format( "[{0}]",string.Join(",",ss) );
    }
  #endregion
}
#endregion